package com.sf.fy.uav.applet.config;

import java.util.List;


import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import jakarta.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.sf.kafka.api.consume.IStringMessageConsumeListener;
import com.sf.kafka.api.consume.KafkaConsumeRetryException;
import com.sf.kafka.exception.KafkaException;

/**
 * @author LTQ
 */

@Component
@Scope("singleton")
public class EasConsumer implements IStringMessageConsumeListener {

    private static final Logger logger = LoggerFactory.getLogger(EasConsumer.class);
    @Value("${config.command_gcm_topic}")
    private String topic;

    @Value("${config.consume_gcm_systemIdToken}")
    private String token;

    @Value("${config.consumer_gcm_group_name}")
    private String UOCS_CONSUMER_GROUP;

    @Value("${config.consumeThreadCount:1}")
    private int threadCount = 1;

    @Value("${config.consume_systemUrl}")
    private String monitorUrl;

    @Value("${config.consumeClusterName}")
    private String clusterName;

    @Value("${config.kafka_consumer_disable:false}")
    private boolean consumerDisable = false;
//
//    @Autowired
//    private AsmMsgService asmMsgService;

    @PostConstruct
    private void init() throws KafkaException {
        if (!consumerDisable) {
            KafkaConsumer kafkaConsumer = new KafkaConsumer(this.UOCS_CONSUMER_GROUP, this.monitorUrl, this.clusterName, topic, this.token, this.threadCount);
            kafkaConsumer.initRegister(this);
            logger.info("UOCS_EasConsumer_CONSUMER_GROUP 初始完成");
        }
    }


    @Override
    public void onMessage(List<String> messages)
            throws KafkaConsumeRetryException {

        if (CollectionUtils.isNotEmpty(messages)) {
            for (String msg : messages) {
//                try {
//                    UavMessage uavMessage = JSON.parseObject(msg, UavMessage.class);
//
//                    if (uavMessage != null) {
//                        logger.info("UOCS_EasConsumer_CONSUMER_GROUP :" + JSON.toJSON(uavMessage));
//                        asmMsgService.reviceAsmMsg(uavMessage);
//                    } else {
//                        logger.error("EasConsumer uavMessage parse error " + uavMessage);
//                    }
//                } catch (Exception e) {
//                    e.printStackTrace();
//                    logger.error("UOCS_EasConsumer_CONSUMER_GROUP error", e);
//                }
            }
        } else {
            logger.error("EasConsumer messages is empty!");
        }


    }
}

